#ifndef QX_HW_SERIAL_H
#define QX_HW_SERIAL_H

#include <rtthread.h>
#include <rtdevice.h>
#include "device.h"
#include "hal/serial/serial.h"

/* 串口 100Kbps 为飞控遥控器的串口通信提供支持 */
#define BAUD_RATE_100000  100000

/* Default config for serial_configure structure */
#define RT_SERIAL_CONFIG_DEFAULT_0         \
{                                          \
    BAUD_RATE_115200, /* 115200 bits/s */  \
    DATA_BITS_8,      /* 8 databits */     \
    STOP_BITS_1,      /* 1 stopbit */      \
    PARITY_NONE,      /* No parity  */     \
    BIT_ORDER_LSB,    /* LSB first sent */ \
    NRZ_NORMAL,       /* Normal mode */    \
    RT_SERIAL_RB_BUFSZ, /* Buffer size */  \
    RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
    0                                      \
}

#define RT_SERIAL_CONFIG_DEFAULT_1         \
{                                          \
    BAUD_RATE_100000, /* 100000 bits/s */  \
    DATA_BITS_8,      /* 8 databits */     \
    STOP_BITS_2,      /* 2 stopbit */      \
    PARITY_EVEN,      /* Even parity  */   \
    BIT_ORDER_LSB,    /* LSB first sent */ \
    NRZ_NORMAL,       /* Normal mode */    \
    RT_SERIAL_RB_BUFSZ, /* Buffer size */  \
    RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
    0                                      \
}


typedef struct{
    DMA_ConfigParams* dma_param;
    rt_uint8_t channel;
}qx_dma_config_t;

struct qx_serial_board_config {
    const char* name;
    volatile struct SCI_REGS *instance;
    INTERRUPT_Type intr_type;
    rt_uint32_t rx_pin_num;
    rt_uint32_t tx_pin_num;
#if defined(RT_SERIAL_USING_DMA)
    DMA_ConfigParams* serial_rx_dma;
    DMA_ConfigParams* serial_tx_dma;
    rt_uint8_t rx_channel;
    rt_uint8_t tx_channel;
#endif
};

struct qx_serial_t{
    struct qx_serial_board_config* board_config;
    rt_uint16_t serial_dma_flag;
    struct rt_serial_device serial_arch;
};

extern rt_err_t rt_hw_serial_init(void);

#endif
